package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttg.common.exception.BaseException;
import com.ttg.model.pojo.BankOrderCreditInfoPrimitive;
import com.ttg.model.pojo.ModelMctMonth;
import com.ttg.model.mapper.ModelMctMonthMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.model.vo.IndustryDepositVO;
import com.ttg.model.vo.IndustryQualityVO;
import com.ttg.web.dao.ModelMctMonthDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @author chenyou
 * @datetime 2025-11-12 17:30:18
 */
@Service
public class ModelMctMonthDaoImpl extends ServiceImpl<ModelMctMonthMapper, ModelMctMonth> implements ModelMctMonthDao {

    @Override
    public boolean deleteByYearAndMonth(String year, String month) {
        LambdaQueryWrapper<ModelMctMonth> w = new LambdaQueryWrapper<>();
        w.eq(ModelMctMonth::getYear,year)
                .eq(ModelMctMonth::getMonth,month);
        return baseMapper.delete(w)>0;
    }

    @Override
    public void efficientBatchSaveOrUpdate(List<ModelMctMonth> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        if(list.size()>1000){
            throw new BaseException("批次更新数量不能超过1000");
        }
        // 1. 查询已存在的记录
        List<ModelMctMonth> existRecords = baseMapper.selectExistByMultiFields(list);
        Map<String, ModelMctMonth> existRecordMap = existRecords.stream()
                .collect(Collectors.toMap(
                        record -> buildUniqueKey(record.getYear(),record.getMonth(),record.getMctNo()),
                        Function.identity()
                ));

        // 2. 分离需要插入和更新的数据
        List<ModelMctMonth> toInsert = new ArrayList<>();
        List<ModelMctMonth> toUpdate = new ArrayList<>();

        for (ModelMctMonth record : list) {
            String uniqueKey = buildUniqueKey(record.getYear(),record.getMonth(),record.getMctNo());
            ModelMctMonth existRecord = existRecordMap.get(uniqueKey);

            if (existRecord != null) {
                // 设置ID用于更新
                record.setId(existRecord.getId());
                toUpdate.add(record);
            } else {
                toInsert.add(record);
            }
        }

        // 3. 批量操作
        if (!toInsert.isEmpty()) {
            this.saveBatch(toInsert);
        }
        if (!toUpdate.isEmpty()) {
            this.updateBatchById(toUpdate);
        }
    }

    @Override
    public List<Long> getMctNoList(String year, String month) {
        LambdaQueryWrapper<ModelMctMonth> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ModelMctMonth::getYear,year)
                .eq(ModelMctMonth::getMonth,month);
        wrapper.select(ModelMctMonth::getMctNo);
        List<ModelMctMonth> list = list(wrapper);
        if(CollectionUtils.isEmpty(list)){
          return null;
        }
        return list.stream().map(o -> o.getMctNo()).collect(Collectors.toList());
    }

    @Override
    public List<Long> getMctNoByClsId2(String year, String month, String clsId2) {
        LambdaQueryWrapper<ModelMctMonth> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ModelMctMonth::getYear,year)
                .eq(ModelMctMonth::getMonth,month)
                .eq(ModelMctMonth::getClsId2,clsId2);
        wrapper.select(ModelMctMonth::getMctNo);
        List<ModelMctMonth> list = list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.stream().map(o -> o.getMctNo()).collect(Collectors.toList());
    }

    @Override
    public List<ModelMctMonth> getByMctNos(String year, String month, List<Long> mctNos) {
        LambdaQueryWrapper<ModelMctMonth> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ModelMctMonth::getYear,year)
                .eq(ModelMctMonth::getMonth,month)
                .in(ModelMctMonth::getMctNo,mctNos);
        List<ModelMctMonth> list = list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    @Override
    public IndustryQualityVO getIndustryQuality(String year, String month, String clsId2,Integer livenessDays) {
        return baseMapper.getIndustryQuality(year,month,clsId2,livenessDays);
    }

    @Override
    public IndustryDepositVO getIndustryDeposit(String year, String month, String clsId2, Integer livenessDays, Integer effectDepMctMinAmt, Integer highDepMctMinAmt) {
        return baseMapper.getIndustryDeposit(year, month, clsId2, livenessDays, effectDepMctMinAmt, highDepMctMinAmt);
    }

    private String buildUniqueKey(String year, String month, Long mctNo) {
        return year + "_" + month + "_" + mctNo;
    }
}

